﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using RibbonLib;
using pub.Functions;
using pub.MRU;
using todo.Persistence;

namespace todo
{
    partial class FrmMain
    {
        private MruItemList _mru;

        private void InitMru()
        {
            //
            _mru = new MruItemList {
                    Persister = new MruPersister(App.Settings["Recent Files"]),
                    MaxCount = 9
            };

            setupMruMenuItems();
        }

        private void SetupRecentItems()
        {
            var items = new List<RecentItemsPropertySet>();
            for (var n = 0; n < _mru.ItemCount; n++)
            {
                var item = _mru.ItemAt(n);
                var filename = item.Value;

                var label = Path.GetFileName(filename);
                //
                var title = TitleParser.Parse(filename);
                if (!string.IsNullOrEmpty(title))
                {
                    label = label + " - " + title;
                }

                items.Add(new RecentItemsPropertySet {
                        Label = label,
                        LabelDescription = filename,
                        Pinned = item.Pinned
                });
            }
            if (_recentItems != null)
            {
                _recentItems.Items = items;
            }
        }

        private void setupMruMenuItems()
        {
            var sep = sepRecentFiles;
            var sepIndex = sep.Index;
            while (true)
            {
                var index = sepIndex + 1;
                var nextItem = menuFile.MenuItems[index];
                if (nextItem.Text == "-")
                {
                    break;
                }
                menuFile.MenuItems.RemoveAt(index);
            }
            var count = Math.Min(_mru.ItemCount, 4);
            sepRecentFiles.Visible = count > 0;
            for (var n = 0; n < count; n++)
            {
                var item = _mru.ItemAt(n);

                var caption = "&" + (n + 1) + ". " + PathFuncs.CompactPath(item.Value, 32);
                var menuItem = new MenuItem(caption);
                menuItem.Tag = item.Value;
                menuItem.Click += (sender, args) => {
                    var fileName = (sender as MenuItem).Tag as string;
                    if (!File.Exists(fileName))
                    {
                        OnMruItemNotFound(fileName);
                    }
                    else
                    {
                        OnOpen(fileName);
                    }
                };
                menuFile.MenuItems.Add(sepIndex + 1 + n, menuItem);
            }
        }

        private void OnMruItemNotFound(string fileName)
        {
            var msg = "'" + fileName + "' was not found.\n" +
                      "Do you want to remove the file name from the recent files list?";
#if zh
            msg = "'" + fileName + "' 未找到。\n" +
                        "要把该文件名从最近打开列表中移除吗？";
#endif
            var dlgResult = MessageBox.Show(msg, App.Title,
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dlgResult == DialogResult.Yes)
            {
                _mru.Remove(fileName);
                setupMruMenuItems();
                SetupRecentItems();
            }
        }

        private void UseFile(string filePath)
        {
            if (_mru.Use(filePath))
            {
                setupMruMenuItems();
                SetupRecentItems();
            }
        }
    }
}